import os
from openai import OpenAI
import json

client = OpenAI(
    api_key = "",
    base_url = "https://ark.cn-beijing.volces.com/api/v3",
)


def process_json_files(folder_path, output_folder):
    # Ensure the output folder exists
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # 遍历input文件夹
    for filename in os.listdir(folder_path):
        if filename.endswith('.json'):
            file_path = os.path.join(folder_path, filename)
            output_path = os.path.join(output_folder, filename)  # Set the output path to the new folder
            
            try:
                # 读取JSON文件
                with open(file_path, 'r', encoding='utf-8') as f:
                    original_data = f.read()
                
                # 构造AI请求
                messages = [
                    {
                        "role": "system",
                        "content": "你是一个专业的JSON数据处理助手，擅长补全对话数据"
                    },
                    {
                        "role": "user",
                        "content": '''请执行以下操作：
1. 将以下数据转换为标准JSON格式
2. 补全output字段或input为null的项，去除完全null项。
3. 保持原有数据结构
4. 输出必须是纯JSON（不带注释和代码块）
5. 输出的json仅包含2个层级，例如[{}]

输入数据：
'''+original_data
                    }
                ]

                # 调用AI接口
                completion = client.chat.completions.create(
                    model="ep-20250206141936-8l6xp",
                    messages=messages,
                    temperature=0.3  # 控制生成稳定性
                )

                # 处理响应
                response = completion.choices[0].message.content
                
                # 清理响应中的代码块标记
                if '```json' in response:
                    response = response.split('```json')[1].split('```')[0].strip()
                elif '```' in response:
                    response = response.split('```')[1].split('```')[0].strip()

                # 解析并保存结果到output2文件夹
                processed_data = json.loads(response)
                with open(output_path, 'w', encoding='utf-8') as f:
                    json.dump(processed_data, f, ensure_ascii=False, indent=2)
                
                print(f"成功处理文件：{filename}")

            except Exception as e:
                print(f"处理文件 {filename} 时出错：{str(e)}")

if __name__ == "__main__":
    input_folder = "output"  # 替换为实际输入文件夹路径
    output_folder = "output2"  # 设置输出文件夹
    process_json_files(input_folder, output_folder)
